package cranix.board.dao;

import java.util.ArrayList;

import cranix.common.bc.SuperBc;
import cranix.common.dao.SuperDao;
import cranix.common.util.Box;
public class CodeDao extends SuperDao {
	private String INSERT_QUERY = "insert into codedb(class_id,code_id,code_id2,code_id3,value,value_int,value_text) values(?,?,?,?,?,?,?)";
	private String UPDATE_QUERY = "update codedb set class_id=?,code_id=?,code_id2=?,code_id3=?,value=?,value_int=?,value_text=? where seq=?";
	private String LIST_QUERY = "select * from codedb order by seq desc limit ? , ?";
	private String COUNT_QUERY = "select count(*) maxcnt from codedb";

	public CodeDao(SuperBc sbc) {
		super(sbc);
	}
	
	public int getMaxCount() throws Exception {
		int count = 0;		
		Box<String,Object> result = executeQuery(COUNT_QUERY);
		count = result.getRow(0).getInt("maxcnt");
		return count;
	}
	@SuppressWarnings( "unchecked" )	
	public Box<String,Object> getCodePage() throws Exception {
		Box<String,Object> input = getInputBox().getBox("paramBox");
		
		int pagePerList = input.getInt("ppl");
		int page = input.getInt("page");
		
		int endIdx = page * pagePerList;
		int startIdx = endIdx - pagePerList;
		
		ArrayList params = new ArrayList();
		params.add(startIdx);
		params.add(endIdx);
		
		return executeQuery(LIST_QUERY,params);
	}
	
	@SuppressWarnings("unchecked")
	public int insertCode() throws Exception {
		Box<String,Object> input = getInputBox().getBox("paramBox");
		
		ArrayList params = new ArrayList();
		
		params.add(input.getString("class_id"));
		params.add(input.getString("code_id"));
		params.add(input.getString("code_id2"));
		params.add(input.getString("code_id3"));
		params.add(input.getString("value"));
		params.add(input.getInt("value_int"));
		params.add(input.getString("value_text"));

		return executeUpdate(INSERT_QUERY,params);
	}
	@SuppressWarnings("unchecked")
	public int updateCode() throws Exception {
		Box<String,Object> input = getInputBox().getBox("paramBox");
		
		ArrayList params = new ArrayList();
		
		params.add(input.getString("class_id"));
		params.add(input.getString("code_id"));
		params.add(input.getString("code_id2"));
		params.add(input.getString("code_id3"));
		params.add(input.getString("value"));
		params.add(input.getInt("value_int"));
		params.add(input.getString("value_text"));
		params.add(input.getInt("seq"));

		return executeUpdate(UPDATE_QUERY,params);
	}	
}
